<template>
  <div
    class="page_body"
    v-loading.fullscreen.lock="loadingKey"
    :element-loading-text="loadingText"
  >
    <!-- 面包屑 -->
    <el-row>
      <el-col :span="24" class="page_box_top">
        <i class="el-icon-s-fold"></i>
        <el-breadcrumb separator="/" class="page_box_top_nav">
          <el-breadcrumb-item class="breadcrumb_active_item"
            >考试管理</el-breadcrumb-item
          >
          <el-breadcrumb-item>考点管理</el-breadcrumb-item>
        </el-breadcrumb>
      </el-col>
    </el-row>
    <el-row>
      <div class="area_manager_content">
        <!-- 按钮栏 -->
        <el-row>
          <el-col :span="24">
            <div class="area_manager_top">
              <el-button size="mini" type="primary" @click="addAreaFn()"
                >新增</el-button
              >
            </div>
          </el-col>
        </el-row>
        <!-- 数据列表 -->
        <div v-if="areaListData != undefined && areaListData.length > 0">
          <el-row>
            <el-col :span="24">
              <el-table
                :data="areaListData"
                border
                size="mini"
                style="width: 100%"
              >
                <el-table-column
                  type="index"
                  label="序号"
                  width="50"
                  align="center"
                >
                </el-table-column>
                <el-table-column
                  prop="area"
                  label="考点名称"
                  align="center"
                ></el-table-column>
                <el-table-column
                  prop="sequence"
                  label="排序"
                  width="80"
                  align="center"
                >
                </el-table-column>
                <el-table-column
                  prop="spell"
                  label="简称"
                  width="100"
                  align="center"
                >
                </el-table-column>
                <el-table-column
                  prop="created_at"
                  label="创建时间"
                  align="center"
                ></el-table-column>
                <el-table-column
                  prop="updated_at"
                  label="更新时间"
                  align="center"
                ></el-table-column>
                <el-table-column
                  label="操作"
                  width="260"
                  itemprop=""
                  align="center"
                >
                  <template slot-scope="scope">
                    <el-button
                      size="mini"
                      type="primary"
                      @click="detailFn(scope.$index, scope.row)"
                      >查看</el-button
                    >
                    <el-button
                      size="mini"
                      type="primary"
                      @click="editFn(scope.$index, scope.row)"
                      >编辑</el-button
                    >
                    <el-button
                      size="mini"
                      type="danger"
                      @click="deleteFn(scope.$index, scope.row)"
                      >删除</el-button
                    >
                  </template>
                </el-table-column>
              </el-table>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <div class="area_manager_pagination">
                <el-pagination
                  @size-change="listSizeChange"
                  @current-change="listCurrentChange"
                  :current-page="currentPage"
                  :page-sizes="pageSizes"
                  :page-size="pageSize"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="total"
                >
                </el-pagination>
              </div>
            </el-col>
          </el-row>
        </div>
        <el-row v-else>
          <el-empty description="暂无数据"></el-empty>
        </el-row>
      </div>
    </el-row>
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="500px"
      :before-close="dialogClose"
    >
      <el-row>
        <el-col :span="24">
          <el-form
            ref="submitFrom"
            :model="submitData"
            label-width="100px"
            :rules="rules"
            :disabled="dialogEditKey == 3"
          >
            <el-form-item label="考点名称" size="small" prop="area">
              <el-col :span="24">
                <el-input v-model="submitData.area" maxlength="8"></el-input>
              </el-col>
            </el-form-item>

            <el-form-item label="简称" size="small" prop="spell">
              <el-col :span="24">
                <el-input v-model="submitData.spell" maxlength="8"></el-input>
              </el-col>
            </el-form-item>

            <el-form-item label="排序" size="small" prop="sequence">
              <el-col :span="24">
                <el-input
                  type="number"
                  v-model="submitData.sequence"
                  maxlength="4"
                ></el-input>
              </el-col>
            </el-form-item>
            <el-form-item size="small" v-if="dialogEditKey != 3">
              <el-col :span="24">
                <div class="area_manager_dialog_tips">* 排序数值越小越靠前</div>
              </el-col>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>

      <el-row type="flex" justify="center">
        <el-col :span="24">
          <div class="area_manager_dialog_btn_box" v-if="dialogEditKey != 3">
            <el-button
              size="small"
              type="primary"
              @click="dialogSubmitBtn()"
              style="margin-right: 30px"
              >确认</el-button
            >
            <el-button size="small" type="danger" @click="dialogClose()"
              >取消</el-button
            >
          </div>
          <div class="area_manager_dialog_btn_box" v-else>
            <el-button size="small" type="primary" @click="dialogClose()"
              >确认</el-button
            >
          </div>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>
<style src="../../assets/css/exam/areaManager.css" scoped></style>

<script>
export default {
  name: "AreaManager",
  components: {},
  data() {
    return {
      areaListData: [], // 考点列表
      currentPage: 1, // 当前页
      pageSizes: [10, 20, 50, 100], // 可选页容量
      pageSize: 10, // 当前页容量
      total: 0, // 总条数
      dialogTitle: "", // 弹窗标题
      dialogVisible: false, // 弹窗显示
      dialogEditId: "", // 编辑id
      dialogEditKey: 0, // 是否编辑
      submitData: {
        area: "",
        sequence: "",
        spell: "",
      }, // 弹框提交数据
      rules: {
        area: [
          { required: true, message: "请输入考点名称", trigger: "change" },
        ],
        spell: [{ required: true, message: "请输入简称", trigger: "change" }],
      }, // 校验规则
      loadingKey: false,
      loadingText: "",
    };
  },
  created: function () {
    this.getListDataFn();
  },
  methods: {
    // 获取列表数据
    getListDataFn: function () {
      let that = this;
      let url = "/admin/area/index";
      let data = {
        page: this.currentPage,
        perPage: this.pageSize,
      };
      that.loadingText = "加载中...";
      that.loadingKey = true;
      this.axios
        .post(url, data)
        .then((response) => {
          that.loadingKey = false;
          if (response.code == 0) {
            that.areaListData = response.area_list;
            that.total = response.total_count;
          } else {
            that.$message.error(response.msg);
          }
        })
        .catch(function (error) {
          that.loadingKey = false;
          console.log(error);
        });
    },
    // 新增
    addAreaFn: function () {
      this.dialogTitle = "新增考点";
      this.dialogEditId = "";
      this.dialogEditKey = 2;
      this.submitData = {
        area: "",
        sequence: "",
        spell: "",
      };
      this.$nextTick(function () {
        this.dialogVisible = true;
        this.$refs.submitFrom.clearValidate();
      });
    },
    // 修改
    editFn: function (index, row) {
      this.dialogTitle = "编辑考点";
      this.dialogEditId = row.id;
      this.dialogEditKey = 1;
      this.submitData = {
        area: row.area,
        sequence: row.sequence,
        spell: row.spell,
      };
      this.$nextTick(function () {
        this.dialogVisible = true;
      });
    },
    // 查看
    detailFn: function (index, row) {
      this.dialogTitle = "查看考点";
      this.dialogEditId = row.id;
      this.dialogEditKey = 3;
      this.submitData = {
        area: row.area,
        sequence: row.sequence,
        spell: row.spell,
      };
      this.$nextTick(function () {
        this.dialogVisible = true;
      });
    },

    // 删除
    deleteFn: function (index, row) {
      let that = this;
      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(() => {
        let url = "/admin/area/destroy";
        let data = {
          id: row.id,
        };
        that.loadingText = "处理中...";
        that.loadingKey = true;
        that.axios
          .post(url, data)
          .then((response) => {
            that.loadingKey = false;
            if (response.code == 0) {
              that.$message.success("删除成功");
              let next = that.currentPage - 1;
              if (that.areaListData.length == 1) {
                that.currentPage = next >= 1 ? next : 1;
              }
              that.getListDataFn();
            } else {
              that.$message.error(response.msg);
            }
          })
          .catch(function (error) {
            that.loadingKey = false;
            console.log(error);
          });
      });
    },
    // 页容量改变
    listSizeChange: function (val) {
      this.currentPage = 1;
      this.pageSize = val;
      this.getListDataFn();
    },
    // 当前页改变
    listCurrentChange: function (val) {
      this.currentPage = val;
      this.getListDataFn();
    },
    // 关闭弹窗
    dialogClose: function () {
      this.dialogVisible = false;
    },
    // 弹窗提交按钮
    dialogSubmitBtn: function () {
      let that = this;
      let url = "";
      let data = {};
      if (this.dialogEditKey == 1) {
        url = "/admin/area/update";
        data = {
          id: this.dialogEditId,
          area: this.submitData.area,
          sequence: this.submitData.sequence,
          spell: this.submitData.spell,
        };
      } else {
        url = "/admin/area/store";
        data = {
          area: this.submitData.area,
          sequence: this.submitData.sequence,
          spell: this.submitData.spell,
        };
      }
      this.$refs.submitFrom.validate((valid) => {
        if (!valid) {
          return false;
        } else {
          that.loadingText = "处理中...";
          that.loadingKey = true;
          this.axios
            .post(url, data)
            .then((response) => {
              that.loadingKey = false;
              if (response.code == 0) {
                if (that.dialogEditKey == 1) {
                  that.$message.success("修改成功");
                } else {
                  that.$message.success("新增成功");
                }
                that.dialogClose();
                that.getListDataFn();
              } else {
                that.$message.error(response.msg);
              }
            })
            .catch(function (error) {
              that.loadingKey = false;
              console.log(error);
            });
        }
      });
    },
  },
};
</script>
